Interactive bar plots of year and program.
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(ggplot2)
library(tidyverse)
## ── Attaching packages ────────────────────────────────────── tidyverse 1.2.1 ──
## ✓ tibble 2.1.3 ✓ purrr 0.3.3
## ✓ tidyr 1.0.2 ✓ dplyr 0.8.4
## ✓ readr 1.3.1 ✓ stringr 1.4.0
## ✓ tibble 2.1.3 ✓ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks plotly::filter(), stats::filter()
## x dplyr::lag() masks stats::lag()
q2 <- read.table("https://raw.githubusercontent.com/bcaffo/ds4ph-bme/master/data/classInterests.txt", header = T)
# for year
q2_year <- ggplot(q2, aes(Year)) + geom_bar()
ggplotly(q2_year)
# for program
q2_program <- ggplot(q2, aes(Program)) + geom_bar()
ggplotly(q2_program)
Interactive plot of US healthcare spending.
#question regarding US healthcare spending
q5 <- read.csv("https://raw.githubusercontent.com/jhu-advdatasci/2018/master/data/KFF/healthcare-spending.csv",
skip = 2)
#delete rows of notes
q5 <- q5[-(53:61),]
#change the data into long format
q5_long <- gather(q5, year, health_spending,
X1991__Total.Health.Spending:X2014__Total.Health.Spending, factor_key=TRUE)
#set x-axis label
a <- (1991:2014)
q5_byyear <- ggplot(q5_long, aes(x=q5_long$year, y=q5_long$health_spending,
group = Location)) +
geom_line(aes(colour=Location)) +
geom_point(aes(colour=Location)) +
labs(y = "Total Health Spending", x = "Year") +
scale_x_discrete(labels= a) +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
legend.title = element_text(size = 8),
legend.text = element_text(size = 6))
ggplotly(q5_byyear)
q5$mean <- rowMeans(q5[,-1])
q5_bystate <- ggplot(q5, aes(x=q5$Location, y=q5$mean)) +
geom_col() +
labs(x = "State", y = "Average Health Spending") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(q5_bystate)